package com.weddingee.action.home;

import java.util.Date;
import java.util.List;

import com.weddingee.action.base.BaseAction;
import com.weddingee.dao.home.HomeDao;
import com.weddingee.dao.home.OtherDao;
import com.weddingee.dao.user.UserDao;
import com.weddingee.dto.ViewDto;
import com.weddingee.model.User;
import com.weddingee.model.Other;
import com.weddingee.util.Constants;
import com.weddingee.util.DaoFactory;
import com.weddingee.util.cond.UserCondition;

public class HomeAction extends BaseAction {
	private HomeDao homeDao = (HomeDao) DaoFactory.create(1);
	private UserDao userDao = (UserDao) DaoFactory.create(6);
	private OtherDao otherDao = (OtherDao)DaoFactory.create(3);
	private Long userId;
	private String username;
	private String realname;
	private String password;
	private String userType;
	private String headpic;
	private String message1;
	private String message2;
	private String message3;
	private String message4;
	private String message5;
	private String message6;
	private String message7;
	private String introduce;
	private int page;
	private int count;
	private int type;
	private String typeStr;
	private Long sex;
	private ViewDto view = new ViewDto();
	private List<User> userList;
	private int totalPage;		//当前查询条件的总数
	private List<Other> otherList;
	
	public Long getSex() {
		return sex;
	}

	public void setSex(Long sex) {
		this.sex = sex;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String home() {
		if( 0 == page ){
			page = 1;
		}
		if( 0 == count ){
			count = 54;
		}
		UserCondition cond = new UserCondition();
		cond.setPage(page);
		cond.setCount(count);
		type=type;
		typeStr = getCNType();
		if(null==typeStr){
			typeStr = "婚礼司仪";
		}
		cond.setType(typeStr);
		userList = userDao.listUser(cond);
		
		int totalCount = userDao.listUserTotalCount(cond);
		if( totalCount%count != 0 )
		{
			totalPage = totalCount/count + 1;
		}else{
			totalPage = totalCount/count;
		}
		return SUCCESS;
	}
	
	public String other() {
		if( 0 == page ){
			page = 1;
		}
		if( 0 == count ){
			count = 54;
		}
		type=type;
		UserCondition cond = new UserCondition();
		cond.setPage(page);
		cond.setCount(count);
		
		typeStr = getCNType();
		if(null==typeStr){
			typeStr="婚礼租车";
		}
		cond.setType(typeStr);
		otherList = otherDao.listOther(page, typeStr);
		int totalCount = otherDao.listOtherTotalCount(typeStr);
		
		if( totalCount%count != 0 )
		{
			totalPage = totalCount/count + 1;
		}else{
			totalPage = totalCount/count;
		}
		System.out.print(totalCount);
		return SUCCESS;
	}

	private String getCNType() {
		if( Constants.USER_TYPE_HOST == type ){
			return "婚礼家";
		}else if( Constants.USER_TYPE_MAKEUP_ARTIST == type ){
			return "婚礼彩妆";
		}else if( Constants.USER_TYPE_CAMERAMAN == type ){
			return "婚礼摄像";
		}else if( Constants.USER_TYPE_MASTER_OF_CEREMONY == type ){
			return "婚礼司仪";
		}else if( Constants.USER_TYPE_PHOTOGRAPHER == type ){
			return "婚礼摄影";
		}
		else if( Constants.OTHER_TYPE_DESIGN == type ){
			return "婚礼设计";
		}else if( Constants.OTHER_TYPE_VIDEO == type ){
			return "婚礼视频";
		}else if( Constants.OTHER_TYPE_TOOL == type ){
			return "婚礼道具";
		}else if( Constants.OTHER_TYPE_CAR == type ){
			return "婚礼租车";
		}
		else{
			
		}
		return null;
	}

//register
  public String register(){
	  User user = new User();
	  User user1 = userDao.findByUsername(username);
	  if(user1!=null){
		  return SUCCESS;
	  }
	  user.setUsername(username);
	  user.setRealname(realname);
	  user.setPassword(password);
	  user.setType(userType);
	  user.setSex(sex);
	  Date createTime = new Date();
	  user.setCreateTime(createTime);
	  user.setHeadPic("/images/head.jpg");
	  user=userDao.add(user);
	  session.put(Constants.SESSION_KEY_LOGIN_USER,user);
	  return SUCCESS;
  }

	// delete
	public String delete() {
		homeDao.delete(userId);
		return SUCCESS;

	}
	
	// find
	public String find() {
		User user=userDao.findByID(userId);
		view.setData(user);
		view.setMsg(SUCCESS);
		return SUCCESS;
	}

	// login
	public String login() {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		String satete = homeDao.login(user);
		return SUCCESS;
	}

	// list
	public String list() {
		if (page == 0) {
			page = 1;
		}
		List<User> userList = homeDao.listUser(page, userType);
		return SUCCESS;
	}

	// update
	public String update() {
		User user = userDao.findByID(userId);
		user.setType(userType);
		user.setIntroduce(introduce);
		if (null != realname) {
			user.setRealname(realname);
		}
		if (null != password) {
			user.setPassword(password);
		}
		if (null != headpic) {
			user.setHeadPic(headpic);
		}
		if (null != message1) {
			user.setProfile1(message1);
		}
		if (null != message2) {
			user.setProfile2(message2);
		}
		if (null != message3) {
			user.setProfile3(message3);
		}
		if (null != message4) {
			user.setProfile4(message4);
		}
		if (null != message5) {
			user.setProfile5(message5);
		}
		if (null != message6) {
			user.setProfile6(message6);
		}
		if (null != message7) {
			user.setProfile7(message7);
		}
		homeDao.update(user);
		return SUCCESS;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMessage1() {
		return message1;
	}

	public void setMessage1(String message1) {
		this.message1 = message1;
	}

	public String getMessage2() {
		return message2;
	}

	public void setMessage2(String message2) {
		this.message2 = message2;
	}

	public String getMessage3() {
		return message3;
	}

	public void setMessage3(String message3) {
		this.message3 = message3;
	}

	public String getMessage4() {
		return message4;
	}

	public void setMessage4(String message4) {
		this.message4 = message4;
	}

	public String getMessage5() {
		return message5;
	}

	public void setMessage5(String message5) {
		this.message5 = message5;
	}

	public String getMessage6() {
		return message6;
	}

	public void setMessage6(String message6) {
		this.message6 = message6;
	}

	public void setHomeDao(HomeDao homeDao) {
		this.homeDao = homeDao;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getHeadpic() {
		return headpic;
	}

	public void setHeadpic(String headpic) {
		this.headpic = headpic;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setCount(int count) {
		this.count = count;
	}

	
	public int getType() {
		return type;
	}

	public ViewDto getView() {
		return view;
	}

	public List<Other> getOtherrList() {
		return otherList;
	}

	public List<Other> getOtherList() {
		return otherList;
	}
	
	public void setType(int type) {
		this.type = type;
	}
	

	public int getJtype(){
		return type;
	}

	public String getTypeStr() {
		return typeStr;
	}

	public void setTypeStr(String typeStr) {
		this.typeStr = typeStr;
	}

	public String getMessage7() {
		return message7;
	}

	public void setMessage7(String message7) {
		this.message7 = message7;
	}

	public String getIntroduce() {
		return introduce;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	public int getTotalPage() {
		return totalPage;
	}
}
